package com.example.dingdingtools

import android.accessibilityservice.AccessibilityService
import android.accessibilityservice.AccessibilityServiceInfo
import android.text.TextUtils
import android.util.Log
import android.view.accessibility.AccessibilityEvent
import android.view.accessibility.AccessibilityNodeInfo
import com.example.tools.PerformClickUtils
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.util.*


class MyAccessibilityService : AccessibilityService() {
    private val TAG = "MyAccessibilityService"

    companion object {
        val workBtn = "com.alibaba.android.rimet:id/home_bottom_tab_button_work"
        var workHelpList = "智能工作助理"
        var edittext = "com.alibaba.android.rimet:id/et_sendmessage"
        var sendBtn = "com.alibaba.android.rimet:id/btn_send"
    }

    override fun onCreate() {
        super.onCreate()
        Log.d(TAG, "onCreate");
        //从系统设置里面开启辅助功能时回调1
    }

    override fun onServiceConnected() {
        super.onServiceConnected()
        Log.d(TAG, "onServiceConnected");
        //从系统设置里面开启辅助功能时回调2
        val serviceInfo = AccessibilityServiceInfo()
        serviceInfo.eventTypes = AccessibilityEvent.TYPES_ALL_MASK
        serviceInfo.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC
        serviceInfo.packageNames = arrayOf("com.alibaba.android.rimet")
        serviceInfo.notificationTimeout = 100
        setServiceInfo(serviceInfo)
    }

    override fun onInterrupt() {
        Log.d(TAG, "onInterrupt");
    }

    override fun onAccessibilityEvent(event: AccessibilityEvent?) {
        //当有事件被监听到时，会回调该方法，AccessibilityEvent 里包含事件类型，发生源头等，
        var eventType = event?.eventType
        // 输出事件的字符串type
        var typeStr = eventType.toString()
        val nodeInfo: AccessibilityNodeInfo? = event?.source ?: return
        try {
            parserTree(nodeInfo)
        } catch (e: Exception) {
            e.printStackTrace();
        }
    }

    var isClick = false
    private fun parserTree(nodeInfo: AccessibilityNodeInfo?) {
        var buttonNode = ArrayList<AccessibilityNodeInfo>()
        if (nodeInfo != null) {
            var childCount = nodeInfo.childCount
            if (childCount >= 1) {
                for (index in 0 until childCount - 1) {
                    val child = nodeInfo.getChild(index)
                    parserTree(child)
                }
            } else {
                var text = nodeInfo.text
                Log.e("TAG", "=11111=======>" + text)
                if (!TextUtils.isEmpty(text) && text.contains(workHelpList)) {
                    Log.e("TAG", "=2222=======>" + text.contains(workHelpList))
                    GlobalScope.launch {
                        delay(2000)
                        PerformClickUtils.findTextAndClick(
                            this@MyAccessibilityService,
                            workHelpList
                        )
                        workHelpList = ""

                        delay(2000)
                        PerformClickUtils.findEditText(nodeInfo, "打卡", application)
                        PerformClickUtils.findTextAndClick(this@MyAccessibilityService, sendBtn)

                        delay(3000)
                        recycle(nodeInfo!!)
                        PerformClickUtils.findTextAndClick(this@MyAccessibilityService, "上班打卡")
                        PerformClickUtils.findTextAndClick(this@MyAccessibilityService, "下班打卡")
                    }

                    return
                }
            }
        }


    }

    fun recycle(info: AccessibilityNodeInfo) {
        if (info.childCount == 0) {

        } else {
            for (i in 0 until info.childCount) {
                if (info.getChild(i).className == "android.support.v7.widget.RecyclerView") {
                    info.getChild(i).performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD)
                    GlobalScope.launch {
                        delay(300)
                        val nodeInfo_ = rootInActiveWindow
                        val list_ = nodeInfo_.findAccessibilityNodeInfosByText("立即打卡")
                        for (acc in list_) {
                            Log.e("TAG", "=-=======>点击" + acc.text)
                        }
                        //滑动之后，上一页的最后一个item为当前的第一个item，所以从第二个开始打招呼
                        if (list_.size > 0) {
                            list_[0].performAction(AccessibilityNodeInfo.ACTION_CLICK)
                            list_[0].parent.performAction(AccessibilityNodeInfo.ACTION_CLICK)
                        }
                    }
                } else {
                    if (info.getChild(i) != null) {
                        recycle(info.getChild(i))
                    }
                }
            }
        }
    }
}